package com.tjmc.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjmc.finance.mapper.PlatformUserAccountMapper;
import com.tjmc.finance.service.MemberInfoService;
import com.tjmc.finance.service.PlatformUserAccountService;
import com.tjmc.finance.service.UsageRecordsService;
import com.tjmc.recruit.common.core.constant.CommonConstants;
import com.tjmc.recruit.common.core.exception.ErrorCodes;
import com.tjmc.recruit.common.core.util.MsgUtils;
import com.tjmc.recruit.common.core.util.R;
import com.tjmc.recruit.finance.api.dto.AccountChangeDTO;
import com.tjmc.recruit.finance.api.entity.PlatformUserAccountEntity;
import com.tjmc.recruit.finance.api.entity.UsageRecordsEntity;
import com.tjmc.recruit.finance.api.enums.ChangeMehtodEnum;
import com.tjmc.recruit.finance.api.enums.UsageServiceTypeEnum;
import com.tjmc.recruit.finance.api.enums.UsageTypeEnum;
import com.tjmc.recruit.finance.api.vo.MemberInfoVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 平台用户账户表
 *
 * @author recruit
 * @date 2024-11-25 14:18:09
 */
@Service
@RequiredArgsConstructor
public class PlatformUserAccountServiceImpl extends ServiceImpl<PlatformUserAccountMapper, PlatformUserAccountEntity> implements PlatformUserAccountService {
    private final UsageRecordsService usageRecordsService;
    private final MemberInfoService memberInfoService;
    @Override
    public R<PlatformUserAccountEntity> getAccountByAccountId(Long accountId) {
        PlatformUserAccountEntity platformUserAccount = baseMapper.selectOne(new LambdaQueryWrapper<PlatformUserAccountEntity>().eq(PlatformUserAccountEntity::getAccountId, accountId));
        if (platformUserAccount == null) {
            return R.failed("未查询到账户信息");
        }
//        //查询
//        List<String> typeList = new ArrayList<>();
//        typeList.add(UsageTypeEnum.JOB_REFRESH.getValue());
//        typeList.add(UsageTypeEnum.JOB_PUBLISH.getValue());
//        typeList.add(UsageTypeEnum.RESUME_REFRESH.getValue());
//        List<UsageRecordsEntity> recordsEntityList = usageRecordsService.list(new LambdaQueryWrapper<UsageRecordsEntity>()
//                .eq(UsageRecordsEntity::getCreatedAt, new Date())
//                .eq(UsageRecordsEntity::getUsageAccountId, platformUserAccount.getAccountId())
//                .in(UsageRecordsEntity::getServiceType, typeList));
//
//        if (!recordsEntityList.isEmpty()) {
//            Map<UsageServiceTypeEnum, List<UsageRecordsEntity>> map = recordsEntityList.stream().collect(Collectors.groupingBy(UsageRecordsEntity::getServiceType));
//            List<UsageRecordsEntity> resumeList = map.get(UsageTypeEnum.RESUME_REFRESH);
//            List<UsageRecordsEntity> publishList = map.get(UsageTypeEnum.JOB_PUBLISH);
//            List<UsageRecordsEntity> refreshList = map.get(UsageTypeEnum.JOB_REFRESH);
////            platformUserAccount.setJobDayPublishNum(publishList.size());
//            platformUserAccount.setJobDayRefreshNum(refreshList.size());
//            platformUserAccount.setResumeDayRefreshNum(resumeList.size());
//        }

        // 会员信息
        R<MemberInfoVO> memberInfo = memberInfoService.getMemberByUserId(accountId);
        MemberInfoVO infoVO = memberInfo.getData();
        platformUserAccount.setMemberInfoVO(infoVO);
        return R.ok(platformUserAccount);
    }

    @Override
    public R<Boolean> changeAccountInfo(AccountChangeDTO changeDTO) {
        // 1. 查询用户账户信息
        R<PlatformUserAccountEntity> data = this.getAccountByAccountId(changeDTO.getAccountId());
        PlatformUserAccountEntity account = data.getData();
        if (account == null) {
            return R.failed("未找到账户信息");
        }
        // 2. 根据用户消费类型扣减账户对应数据
        switch (changeDTO.getUsageType()) {
            case E_COIN -> {
                if (changeDTO.getChangeMethod().equals(ChangeMehtodEnum.ADD.name())) {
                    account.setECoinNum(account.getECoinNum().add(changeDTO.getChangeValue()));
                } else {
                    if (account.getECoinNum().compareTo(changeDTO.getChangeValue()) < 0) {
                        return R.failed("账户E币余额不足");
                    }
                    account.setECoinNum(account.getECoinNum().subtract(changeDTO.getChangeValue()));
                }
            }
            case SMS -> {
                if (changeDTO.getChangeMethod().equals(ChangeMehtodEnum.ADD.name())) {
                    account.setSmsNum(new BigDecimal(account.getSmsNum()).add(changeDTO.getChangeValue()).intValue());
                } else {
                    if (new BigDecimal(account.getSmsNum()).compareTo(changeDTO.getChangeValue()) < 0) {
                        return R.failed("账户短信数量不足");
                    }
                    account.setSmsNum(new BigDecimal(account.getSmsNum()).subtract(changeDTO.getChangeValue()).intValue());
                }
            }
            case CREATE_SUB_ACCOUNT ->{
                if (changeDTO.getChangeMethod().equals(ChangeMehtodEnum.ADD.name())) {
                    account.setSubAccountNum(new BigDecimal(account.getSubAccountNum()).add(changeDTO.getChangeValue()).intValue());
                } else {
                    if (new BigDecimal(account.getSubAccountNum()).compareTo(changeDTO.getChangeValue()) < 0) {
                        return R.failed("账户子账号数量不足");
                    }
                    account.setSubAccountNum(new BigDecimal(account.getSubAccountNum()).subtract(changeDTO.getChangeValue()).intValue());
                }
            }
        }
        // 3. 更新账户数据
        boolean b = this.updateById(account);
        if (!b) {
            return R.failed("扣减账户数据失败");
        }
        return R.ok(Boolean.TRUE);
    }
}
